package com.zhai.servlet;

import com.zhai.config.RpcConfig;
import com.zhai.invoke.RpcServiceInvok;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class InvokePostServlet extends HttpServlet {

    private RpcConfig rpcConfig;

    public InvokePostServlet(RpcConfig rpcConfig) {
        this.rpcConfig = rpcConfig;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=UTF-8");
        resp.setStatus(HttpServletResponse.SC_OK);

        String[] parameters = req.getParameterValues("parameters[]");
        String[] parameterTypes = req.getParameterValues("parameterTypes[]");
        String method = req.getParameter("method");
        String url = req.getParameter("url");
        String returnType = req.getParameter("returnType");

        //发起泛化调用
        String data = null;
        try {
            RpcServiceInvok rpcServiceInvok = new RpcServiceInvok();
            data = rpcServiceInvok.rpcGenericInvok(rpcConfig, url, method, parameterTypes, parameters, returnType);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        PrintWriter out = resp.getWriter();
        out.println(data);
    }
}
